<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户id" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宣传id" prop="publicizerId">
        <el-input
          v-model="queryParams.publicizerId"
          placeholder="请输入宣传id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否匿名" prop="isAnonymous">
        <el-input
          v-model="queryParams.isAnonymous"
          placeholder="请输入是否匿名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="限时" prop="timeLimit">
        <el-input
          v-model="queryParams.timeLimit"
          placeholder="请输入限时"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="内存限制" prop="memoryLimit">
        <el-input
          v-model="queryParams.memoryLimit"
          placeholder="请输入内存限制"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="额外文件" prop="additionalFileId">
        <el-input
          v-model="queryParams.additionalFileId"
          placeholder="请输入额外文件"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="数量" prop="acNum">
        <el-input
          v-model="queryParams.acNum"
          placeholder="请输入数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="提交数量" prop="submitNum">
        <el-input
          v-model="queryParams.submitNum"
          placeholder="请输入提交数量"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否公开" prop="isPublic">
        <el-input
          v-model="queryParams.isPublic"
          placeholder="请输入是否公开"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="级别" prop="level">
        <el-input
          v-model="queryParams.level"
          placeholder="请输入级别"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="文件流" prop="fileIo">
        <el-input
          v-model="queryParams.fileIo"
          placeholder="请输入文件流"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="宣传时间" prop="publicizeTime">
        <el-date-picker clearable
          v-model="queryParams.publicizeTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择宣传时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="类型" prop="type">
        <el-input
          v-model="queryParams.type"
          placeholder="请输入类型"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配置" prop="vjudgeConfig">
        <el-input
          v-model="queryParams.vjudgeConfig"
          placeholder="请输入配置"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['problem:problem:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['problem:problem:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['problem:problem:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['problem:problem:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="problemList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="宣传id" align="center" prop="publicizerId" />
      <el-table-column label="是否匿名" align="center" prop="isAnonymous" />
      <el-table-column label="描述" align="center" prop="description" :show-overflow-tooltip="true"/>
      <el-table-column label="表单格式" align="center" prop="inputFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="输出格式" align="center" prop="outputFormat" :show-overflow-tooltip="true"/>
      <el-table-column label="示例" align="center" prop="example" :show-overflow-tooltip="true"/>
      <el-table-column label="提示" align="center" prop="limitAndHint" :show-overflow-tooltip="true"/>
      <el-table-column label="限时" align="center" prop="timeLimit" />
      <el-table-column label="内存限制" align="center" prop="memoryLimit" />
      <el-table-column label="额外文件" align="center" prop="additionalFileId" />
      <el-table-column label="数量" align="center" prop="acNum" />
      <el-table-column label="提交数量" align="center" prop="submitNum" />
      <el-table-column label="是否公开" align="center" prop="isPublic" />
      <el-table-column label="级别" align="center" prop="level" />
      <el-table-column label="文件流" align="center" prop="fileIo" />
      <el-table-column label="文件输入流名称" align="center" prop="fileIoInputName" />
      <el-table-column label="文件输出流名称" align="center" prop="fileIoOutputName" />
      <el-table-column label="宣传时间" align="center" prop="publicizeTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.publicizeTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="类型" align="center" prop="type" />
      <el-table-column label="配置" align="center" prop="vjudgeConfig" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['problem:problem:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['problem:problem:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改题目管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="宣传id" prop="publicizerId">
          <el-input v-model="form.publicizerId" placeholder="请输入宣传id" />
        </el-form-item>
        <el-form-item label="是否匿名" prop="isAnonymous">
          <el-input v-model="form.isAnonymous" placeholder="请输入是否匿名" />
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input v-model="form.description" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="表单格式" prop="inputFormat">
          <el-input v-model="form.inputFormat" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="输出格式" prop="outputFormat">
          <el-input v-model="form.outputFormat" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="示例" prop="example">
          <el-input v-model="form.example" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="提示" prop="limitAndHint">
          <el-input v-model="form.limitAndHint" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="限时" prop="timeLimit">
          <el-input v-model="form.timeLimit" placeholder="请输入限时" />
        </el-form-item>
        <el-form-item label="内存限制" prop="memoryLimit">
          <el-input v-model="form.memoryLimit" placeholder="请输入内存限制" />
        </el-form-item>
        <el-form-item label="额外文件" prop="additionalFileId">
          <el-input v-model="form.additionalFileId" placeholder="请输入额外文件" />
        </el-form-item>
        <el-form-item label="数量" prop="acNum">
          <el-input v-model="form.acNum" placeholder="请输入数量" />
        </el-form-item>
        <el-form-item label="提交数量" prop="submitNum">
          <el-input v-model="form.submitNum" placeholder="请输入提交数量" />
        </el-form-item>
        <el-form-item label="是否公开" prop="isPublic">
          <el-input v-model="form.isPublic" placeholder="请输入是否公开" />
        </el-form-item>
        <el-form-item label="级别" prop="level">
          <el-input v-model="form.level" placeholder="请输入级别" />
        </el-form-item>
        <el-form-item label="文件流" prop="fileIo">
          <el-input v-model="form.fileIo" placeholder="请输入文件流" />
        </el-form-item>
        <el-form-item label="文件输入流名称" prop="fileIoInputName">
          <el-input v-model="form.fileIoInputName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="文件输出流名称" prop="fileIoOutputName">
          <el-input v-model="form.fileIoOutputName" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="宣传时间" prop="publicizeTime">
          <el-date-picker clearable
            v-model="form.publicizeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择宣传时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="类型" prop="type">
          <el-input v-model="form.type" placeholder="请输入类型" />
        </el-form-item>
        <el-form-item label="配置" prop="vjudgeConfig">
          <el-input v-model="form.vjudgeConfig" placeholder="请输入配置" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listProblem, getProblem, delProblem, addProblem, updateProblem } from "@/api/problem/problem";

export default {
  name: "Problem",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题目管理表格数据
      problemList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        userId: null,
        publicizerId: null,
        isAnonymous: null,
        description: null,
        inputFormat: null,
        outputFormat: null,
        example: null,
        limitAndHint: null,
        timeLimit: null,
        memoryLimit: null,
        additionalFileId: null,
        acNum: null,
        submitNum: null,
        isPublic: null,
        level: null,
        fileIo: null,
        fileIoInputName: null,
        fileIoOutputName: null,
        publicizeTime: null,
        type: null,
        vjudgeConfig: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        level: [
          { required: true, message: "级别不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询题目管理列表 */
    getList() {
      this.loading = true;
      listProblem(this.queryParams).then(response => {
        this.problemList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        userId: null,
        publicizerId: null,
        isAnonymous: null,
        description: null,
        inputFormat: null,
        outputFormat: null,
        example: null,
        limitAndHint: null,
        timeLimit: null,
        memoryLimit: null,
        additionalFileId: null,
        acNum: null,
        submitNum: null,
        isPublic: null,
        level: null,
        fileIo: null,
        fileIoInputName: null,
        fileIoOutputName: null,
        publicizeTime: null,
        type: null,
        vjudgeConfig: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加题目管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getProblem(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改题目管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateProblem(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProblem(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除题目管理编号为"' + ids + '"的数据项？').then(function() {
        return delProblem(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('problem/problem/export', {
        ...this.queryParams
      }, `problem_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
